package com.common.sensitive;

import java.util.function.Function;

/**
 * <p>脱敏策略</p>
 * @author 大忽悠
 * @create 2022/9/27 21:56
 */
public enum DesensitizationStrategy {
    /**
     * Username sensitive strategy.
     */
    USERNAME(s -> s.replaceAll("(\\S)\\S(\\S*)", "$1*$2")),
    /**
     * Id card sensitive type.
     */
    ID_CARD(s -> s.replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1****$2")),
    /**
     * Phone sensitive type.
     */
    PHONE(s -> s.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2")),
    /**
     * Address sensitive type.
     */
    ADDRESS(s -> s.replaceAll("(\\S{3})\\S{2}(\\S*)\\S{2}", "$1****$2****")),

    /**
     * Password sensitive type.
     */
    PASSWORD(s -> s.replaceAll("[\\s\\S]*", "***"));


    private final Function<String, String> desensitizer;

    DesensitizationStrategy(Function<String, String> desensitizer) {
        this.desensitizer = desensitizer;
    }

    /**
     * @return 返回具体用于脱敏的处理器
     */
    public Function<String, String> desensitizer() {
        return desensitizer;
    }
}
